/*
 * Created on 21-ago-2004
 *
 */
package guru.merlin.core;


import java.io.Serializable;
import java.util.Date;
import java.util.Random;


/**
 * @author cobach
 * @version 2.0
 *
 */
public class Envelope implements Serializable{
    
    /**
     *
     */
    private static final long serialVersionUID = -9090772973516929025L;
    
    private String id;
    
    private Date date;
    
    private Address destination;
    private Address origin;
    
    //private Address metaDestination;
    
    private Serializable content;
    
    private Route destinationRoute;
    private Route originRoute;
    
    private String comment;
    private Throwable throwable;
    private int stamp=0;
    private String replyTo;
    private Envelope envelopeReplyTo;//Anadido por Santiago
    
    private long routingTime;
    
    public Envelope (Serializable cont, Address dest, Address orig){
        destination=dest;
        origin=orig;
        content=cont;
        setID ();
        date=new Date ();
    }
    
    public Envelope replyTo (Serializable cont){
        Envelope reply=new Envelope (cont, origin, destination);
        reply.setReplyTo (this);
        
        reply.setEnvelopeReplyTo (this);
        
        return(reply);
    }
    
    
    public Date getDate () {
        return date;
    }
    public void setDate (Date date) {
        this.date = date;
    }
    
    private void setID (){
        Date fecha = new Date ();
        long numero = fecha.getTime ();
        Random generator = new Random (numero);
        int numero2 = generator.nextInt ();
        id = new Integer (Math.abs (numero2)).toString ();
    }
    
    public String getId (){
        return id;
    }
    
    public String toString (){
        
        String tostr = ("[date=("+date+"), content=("+content+")," +
                "destination=("+destination+")," +
                "origin=("+origin+")," +
                "destinationRoute=("+destinationRoute+")," +
                "originRoute=("+originRoute+")]");
        
        return tostr;
        
    }
    
    public String toStringNoContent (){
        
        String tostr = ("[date=("+date+"), content=(Contenido no visible por seguridad)," +
                "destination=("+destination+")," +
                "origin=("+origin+")," +
                "destinationRoute=("+destinationRoute+")," +
                "originRoute=("+originRoute+")]");
        
        return tostr;
        
    }
    
    public boolean wellFormed (){
        return ((date!=null)&&(content!=null)&&(destination!=null)&&(origin!=null));
    }
    
    public Address getDestination (){
        return(destination);
    }
    
    public void setDestination (Address address){
        destination=address;
    }
    
    public Address getOrigin (){
        return(origin);
    }
    
    public void setOrigin (Address address){
        origin=address;
    }
    
    public Object getContent (){
        return content;
    }
    
    public void setContent (Serializable o){
        content=o;
    }
    
    public Route getDestinationRoute (){
        return(destinationRoute);
    }
    
    public void setDestinationRoute (Route r){
        destinationRoute=r;
    }
    
    public Route getOriginRoute (){
        return(originRoute);
    }
    
    public void setOriginRoute (Route r){
        originRoute=r;
    }
    
    public String getComment (){
        return(comment);
    }
    
    public void setComment (String c){
        comment=c;
    }
    
    public Throwable getThrowable (){
        return(throwable);
    }
    
    public void setThrowable (Throwable t){
        throwable=t;
    }
    
    
    public void setStamp (int s){
        stamp=s;
    }
    
    public int getStamp (){
        return(stamp);
    }
    
    
    public String getReplyTo () {
        return replyTo;
    }
    
    public void setReplyTo (Envelope replyTo) {
        replyTo.content=null;
        this.setReplyTo (replyTo.id);
    }
    
    public void setReplyTo (String replyToEnvelopeId) {
        this.replyTo = replyToEnvelopeId;
    }
    
    /*
    public Address getMetaDestination() {
        return metaDestination;
    }
     
    public void setMetaDestination(Address metaDestination) {
        this.metaDestination = metaDestination;
    }
     */
    public long getRoutingTime () {
        return routingTime;
    }
    
    public void setRoutingTime (long routingTime) {
        this.routingTime = routingTime;
    }
    /**
     * @return Devuelve envelopeReplyTo.
     */
    public Envelope getEnvelopeReplyTo () {
        return envelopeReplyTo;
    }
    /**
     * @param envelopeReplyTo El envelopeReplyTo a establecer.
     */
    public void setEnvelopeReplyTo (Envelope envelopeReplyTo) {
        this.envelopeReplyTo = envelopeReplyTo;
    }
}
